    const { $Toast } = require('../../../utils/iview/base/index');
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';

const app = getApp();


Page({
  data: {
    auth: true,
    isloade: {
      hidden: false
    },
    _baseUrl: app.globalData._baseUrl,
    _imgPath: app.globalData._imgPath,
    _imgPaths: app.globalData._imgPaths,
    _backImg: app.globalData._backImg,
    img: [],
    vehicle:'',
    user_type: 1,
    designer_id: '',
    openid: app.globalData.openid,
    shopName: '',
    shopId: '',
    userInfo: '',
    user: {},
    casIndex:0,
    tindex:0,
    vehtype: { o: [{ id: 1, name: '小型汽车' }, { id: 2, name: '中型汽车' }, { id: 2, name: '大型汽车' }],
               a: ['小型汽车', '中型汽车','大型汽车'] },
    rindex:0,
    vehregion: { o: [{ id: 1, name: '粤' }, { id: 2, name: '浙' }, { id: 2, name: '苏' }],
      a: ['粤', '浙', '苏']
    },
    bindex:0,
    brand: {
      o: [{ id: 1, name: '奔驰' }, { id: 2, name: '雪佛兰' }, { id: 2, name: '捷豹' }],
      a: ['奔驰', '雪佛兰', '捷豹']
    },
    casArray:{o:[{id:1,name:'1'}],a:[1]}
  },
  bindvehtype(e){
    var index = e.detail.value;
    this.setData({
      tindex:index
    })
  },
  bindvehregion(e){
    var index = e.detail.value;
    this.setData({
      rindex: index
    })
  },
  bindBrand(e) {
    var index = e.detail.value;
    this.setData({
      bindex: index
    })
  },
  onLoad(options) {
    var _that = this
    _that.setTitle('车辆认证');
    var user = wx.getStorageSync('user');
    var userInfo = wx.getStorageSync('userInfo');
    var vehicle = options.vehicle ? JSON.parse(options.vehicle) : '';
    console.log(vehicle);
    var car = this.data.brand.a;
    if (vehicle){
      for (var i = 0; i < car.length; i++){
        if (vehicle.car_name == car[i]){
          console.log(i,"ii")
            _that.setData({
              bindex:i,

            })
        }
      }
    }
    var { user } = _that.data;
    _that.setData({
      user: user,
      userInfo: userInfo,
      vehicle: vehicle,
      car_id: vehicle.id,
      miaos:vehicle.car_info
    });

  },
  setms(e){
    var value = e.detail.value;
    this.setData({
      miaos: value
    })
    console.log(this.data.miaos);
  },
  
  onShow: function () {
    var _that = this
    var user = wx.getStorageSync('user');
    var userInfo = wx.getStorageSync('userInfo');

    _that.setData({
      isclick: 0
    })


    // _that.setData({
    //   user: user ? user : '',
    //   openid: userInfo.openid ? userInfo.openid : '',
    //   userInfo: userInfo
    // })
    // _that.isUserInfo();



  },
  // formSubmit(){
  //   wx.navigateTo({
  //     url: '../vehicleList/vehicleList',
  //   })
  // }, 

  formSubmit(e) {
    console.log(e,"输出e")
    var _that = this;

    var isclick = _that.data.isclick;

    if (isclick) {
      return;
    }

    _that.setData({
      isclick: 1
    })

    setTimeout(() => {
      _that.setData({
        isclick: 0
      })
    }, 3000)



    var { value } = e.detail;
   
    _that.setData({
      vehicle: value
    })

    
    var token = wx.getStorageSync("token");
    
    var car_id = _that.data.car_id;
    var tips = '新增';
    if (car_id) {
      value.car_id = car_id;
      tips = '修改';
    }

    
    var net = new NetWork(_that.data._baseUrl);

    value.token = token;
    value.car_info = _that.data.miaos;

    let reg_tpl = /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/;

    for (let i in value) {
      let values = value[i];
      if (!values && i != 'car_info') {
        $Toast({
          content: '内容不能为空',
          type: 'none'
        })
        return;
      }
      if (i == 'car_license' && !reg_tpl.test(values)) {
        $Toast({
          content: '车牌号码不正确!',
          type: 'none'
        })
        return;
      }

    }

    net._request({
      url: '/api/v1/member/addCar',
      method: 'POST',
      data: value
    }, tips, res => {

      setTimeout(() => {
        wx.navigateBack({ changed: true });
      }, 1000)
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {

        app.refreshtoken(token);
        setTimeout(() => {
          $Toast({
            content: "添加失败，请重新添加",
            type: 'warning'
          })
        }, 1500)

      }
      else if (res.data.error_code == 400) {   
        $Toast({
          content: res.data.msg,
          type: 'warning'
        });
      }
    })

  },



  scanCode: function () {
    var that = this;
    wx.scanCode({ //扫描API
      success(res) { //扫描成功
        console.log(res) //输出回调信息
        that.setData({
          scanCodeMsg: res.result
        });
        wx.showToast({
          title: '成功23',
          duration: 1000
        })
      }
    })
  },





  /* 拨打电话 */
  // telPhone() {
  // 	var that = this;
  // 	wx.makePhoneCall({
  // 		phoneNumber: ''
  // 	}, res => {

  // 	});
  // },
  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },

  // onShareAppMessage(str) {
  // 	return {
  // 		title: str,
  // 		path: this.route
  // 	}
  // },
  isUserInfo() {
    var _that = this;
    wx.getSetting({
      success(res) {
        // console.log(res)
        if (!res.authSetting['scope.userInfo']) {
          _that.setData({
            auth: false
          })

        }
      }
    })
  },

  

  userInfo(e) {

    let _that = this;
    let _userInfo = e.detail.userInfo;
    console.log(_userInfo)
    wx.setStorageSync('_userInfo', _userInfo);
    let net = new NetWork(app.globalData._baseUrl);
    let _data = e.detail;


    if (_userInfo) {
      this.setData({
        auth: true,
      })
    }


    wx.login({
      success(res) {
        if (res.code) {
          console.log(res.code, "code");

          net._request({
            url: 'api/v1/login/mini',
            method: 'POST',
            data: {
              code: res.code,
              rawData: _data.rawData,
              signature: _data.signature,
              iv: _data.iv,
              encryptedData: _data.encryptedData
            },
          }, '', res => {
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);

            _that.setData({
              userInfo: user
            })

            wx.setStorageSync('user', {
              user_type: 1
            });
            $Toast({
              content: res.data.msg,
              type: 'success'
            });

          }, res => {
            console.log("失败返回", res);
            app.globalData.user = _userInfo;
            var user = res.data.data;
            wx.setStorageSync('userInfo', user);
            $Toast({
              content: res.data.msg,
              type: 'success'
            });
          });
        }
      }
    })
  },

 
})